package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.dao.BannerMapper;
import com.winshare.saleAssistant.domain.dto.req.*;
import com.winshare.saleAssistant.domain.entity.BannerDO;
import com.winshare.saleAssistant.domain.query.BannerQuery;
import com.winshare.saleAssistant.domain.dto.resp.BannerRespDTO;
import com.winshare.saleAssistant.exception.BusinessException;
import com.winshare.saleAssistant.mapper.BannerMapStruct;
import com.winshare.saleAssistant.service.BannerService;
import com.winshare.saleAssistant.util.Page;
import com.winshare.saleAssistant.util.RequestHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {

    private final BannerMapper bannerMapper;

    public BannerServiceImpl(BannerMapper bannerMapper) {
        this.bannerMapper = bannerMapper;
    }

    @Override
    public void save(BannerCreateDTO bannerCreateDTO) {
        BannerDO bannerDO = BannerMapStruct.INSTANCE.fromBannerCreateDTO(bannerCreateDTO);
        bannerDO.setCreator(RequestHolder.getCurrentUser().getStaffName());
        bannerMapper.saveBanner(bannerDO);
    }

    @Override
    public void update(BannerUpdateDTO bannerUpdateDTO) {
        BannerDO bannerDO = BannerMapStruct.INSTANCE.fromBannerUpdateDTO(bannerUpdateDTO);
        BannerDO bannerDOInSql = bannerMapper.getBanner(bannerDO);
        if(null == bannerDOInSql) {
            throw new BusinessException("当前banner已不存在。");
        }
        bannerDO.setCreator(RequestHolder.getCurrentUser().getStaffName());
        bannerMapper.updateBanner(bannerDO);
    }

    @Override
    public Page<BannerRespDTO> getBannerList(BannerQueryDTO bannerQueryDTO) {
        BannerQuery bannerQuery = BannerMapStruct.INSTANCE.fromBannerQueryDTO(bannerQueryDTO);
        bannerQuery.setOffsetIndex(bannerQueryDTO.getOffsetIndex());
        int totalCount = bannerMapper.queryBannerCount(bannerQuery);
        Integer pageIndex = bannerQueryDTO.getPageIndex();
        Integer pageSize = bannerQueryDTO.getPageSize();
        if(0 == totalCount) {
            return new Page<>(pageIndex,pageSize,totalCount,new ArrayList<>());
        }
        List<BannerDO>  bannerDOList = bannerMapper.findBannerListByConditions(bannerQuery);
        List<BannerRespDTO> bannerRespDTOList = BannerMapStruct.INSTANCE.fromBannerDOList(bannerDOList);
        return new Page<>(pageIndex,pageSize,totalCount,bannerRespDTOList);
    }

    @Override
    public void delete(BannerIdDTO bannerIdDTO) {
        BannerDO bannerDO = BannerMapStruct.INSTANCE.fromBannerIdDTO(bannerIdDTO);
        BannerDO bannerDOInSql = bannerMapper.getBanner(bannerDO);
        if(null == bannerDOInSql) {
            throw new BusinessException("当前banner已不存在。");
        }
        bannerMapper.deleteBanner(bannerDO);
    }

    @Override
    public BannerRespDTO getBannerDetail(BaseIdDTO baseIdDTO) {
        BannerDO bannerDO = BannerMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        BannerDO bannerDOInSql = bannerMapper.getBanner(bannerDO);
        if(null == bannerDOInSql) {
            throw new BusinessException("当前banner已不存在。");
        }
        BannerRespDTO bannerRespDTO = BannerMapStruct.INSTANCE.fromBannerDO(bannerDOInSql);
        return bannerRespDTO;
    }

    @Override
    public void addClickCount(BaseIdDTO baseIdDTO) {
        BannerDO bannerDO = BannerMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        bannerMapper.addClickCount(bannerDO);
    }
}
